package com.sky.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.sky.constant.StatusConstant.ENABLE;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 分页查询套餐
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public Result<PageResult> pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {

        //设置分页页码，和页大小
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());

        //分页查询
        List<SetmealVO> setmealList= setmealMapper.pageQuery(setmealPageQueryDTO);
        Page<SetmealVO> p = (Page<SetmealVO>) setmealList;
        PageResult pageResult = new PageResult(p.getTotal(), p.getResult());
        return Result.success(pageResult);
    }

    /**
     * 新增套餐
     * @param setmealDTO
     * @return
     */
    @Override
    @Transactional
    public Result save(SetmealDTO setmealDTO) {
        //拷贝属性
        Setmeal setmeal = new Setmeal();
        BeanUtil.copyProperties(setmealDTO,setmeal);

        //设置状态
        setmeal.setStatus(ENABLE);

        //插入套餐表
        setmealMapper.save(setmeal);

        //获取数据库生成的套餐id
        Long id = setmeal.getId();

        //获取套餐中包含的菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

        //新增套餐菜品
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(id);
        }
        setmealDishMapper.save(setmealDishes);

        return Result.success();
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @Override
    public Result<SetmealVO> selectById(Long id) {

        //查询套餐
        SetmealVO setmealVO = setmealMapper.selectById(id);

        //查询套餐包含的菜品,并设置菜品属性
        List<SetmealDish> setmealDishes = setmealDishMapper.selectBySetmealId(id);
        setmealVO.setSetmealDishes(setmealDishes);

        return Result.success(setmealVO);
    }

    /**
     * 套餐起售、停售
     * @param status
     * @param id
     * @return
     */
    @Override
    public Result setStatus(Integer status, Long id) {
        Setmeal setmeal = Setmeal.builder().status(status).id(id).build();
        setmealMapper.update(setmeal);
        return Result.success();
    }

    /**
     * 修改套餐
     * @param setmealDTO
     * @return
     */
    @Override
    @Transactional
    public Result update(SetmealDTO setmealDTO) {

        //修改数据库套餐
        Setmeal setmeal = new Setmeal();
        BeanUtil.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);

        //删除原有套餐包含的菜品
        Long setmealId = setmealDTO.getId();
        setmealDishMapper.deleteByDishId(setmealId);

        //获取套餐中包含的菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

        //新增套餐菜品
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealId);
        }
        setmealDishMapper.save(setmealDishes);

        return Result.success();
    }


    /**
     * 根据id批量删除套餐
     * @param ids
     * @return
     */
    @Override
    @Transactional
    public Result deleteByIds(List<Long> ids) {

        //删除套餐
        setmealMapper.deleteByIds(ids);

        //删除套餐包含的菜品
        setmealDishMapper.deleteByDishIds(ids);

        return Result.success();
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
